package co.baiku.boot.web.controller;

import co.baiku.boot.common.message.Message;
import co.baiku.boot.core.orm.request.PagingRequest;
import co.baiku.boot.core.web.controller.BaseController;
import co.baiku.boot.orm.po.Student;
import co.baiku.boot.orm.service.StudentService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/Student")
public class StudentController extends BaseController {
    @Resource
    private StudentService studentService;

    @PostMapping("/page")
    public Message page(@RequestBody PagingRequest request) {
        return Message.result(studentService.findDtoPaging(request));
    }

    @PostMapping("/dto/page")
    public Message dtoPage(@RequestBody PagingRequest request) {
        return Message.result(studentService.findDtoPaging(request));
    }

    @GetMapping("/add")
    public Message add() {
        Student student = new Student();
        student.setUserId("1");
        student.setAmount(new BigDecimal(883.23));
        student.setInAt(LocalDateTime.now());
        student.setStudentNo("SN238123");
        return Message.result(studentService.saveOrUpdate(student));
    }

    @GetMapping("/batchInsert")
    public Message batchInsert() {
        Student student = new Student();
        student.setUserId("2");
        student.setAmount(new BigDecimal(883.23));
        student.setInAt(LocalDateTime.now());
        student.setStudentNo("SN234538123");
        List<Student> objects = Lists.newArrayList();
        objects.add(student);
        return Message.result(studentService.batchInsert(objects));
    }

    @GetMapping("/getDto/{id}")
    public Message dtoPage(@PathVariable Long id) {
        return Message.result(studentService.getDtoById(id));
    }

    @GetMapping("/get/{id}")
    public Message get(@PathVariable Long id) {
        return Message.result(studentService.getById(id));
    }

    @GetMapping("/delete/{id}")
    public Message delete(@PathVariable Long id) {
        studentService.delete(id);
        return Message.success();
    }
}
